package leo.gptBot.config;

import leo.gptBot.GPTBotServer;
import leo.gptBot.interceptors.WebSocketHandshakeInterceptor;
import leo.gptBot.webSocket.WebSocketServer;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * @author ：Leo
 * @date ：Created in 2024/7/22 16:45
 * @description：
 */
@EnableWebSocket
@Configuration
public class WebSocketConfig implements WebSocketConfigurer{
    @Value("${server.servlet.ws-path}")
    private String wsPath;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(new WebSocketServer(), Strings.isBlank(wsPath) ? "/ws" : wsPath)
                .addInterceptors(new WebSocketHandshakeInterceptor())
                .setAllowedOrigins("*");
    }
}
